package com.guigu.www.tangyingmeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.mapper.*;
import com.guigu.www.pojo.*;
import com.guigu.www.tangyingmeng.service.TymBespeakService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TymBespeakServiceImpl implements TymBespeakService {

    @Autowired
    private BespeakMapper bespeakMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private BespeakSetMealMapper mealMapper;

    @Autowired
    private BesSmProMapper besSmProMapper;

    @Autowired
    private BespeakPojectMapper pojectMapper;

    @Autowired
    private BespeakPojectTypeMapper typeMapper;

    @Autowired
    private SingleDayAppointmentMapper singleMapper;


    //分页查询预约列表
    @Override
    public PageVo<Bespeak> selldespeakall(Map<String, String> map) {
        QueryWrapper<Bespeak> wrapper = new QueryWrapper<>();
        wrapper.like("bes_id", map.get("findid")).like("bes_name", map.get("findname")).like("bes_phone", map.get("findphone"));
        wrapper.orderByDesc("bes_time");
        IPage<Bespeak> page = new Page<>(Integer.valueOf(map.get("index")), Integer.valueOf(map.get("pageSize")));
        page = bespeakMapper.selectPage(page, wrapper);
        PageVo<Bespeak> pageVo = new PageVo<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(page.getRecords());
        return pageVo;
    }

    //查看预约详情
    @Override
    public Bespeak appointmentdetails(Integer besid) {
        Bespeak bespeak = bespeakMapper.selectById(besid);
        bespeak.setMember(memberMapper.selectById(bespeak.getMemId()));
        BespeakSetMeal bespeakSetMeal = mealMapper.selectById(bespeak.getBesSmId());
        bespeak.setBespeakSetMeal(bespeakSetMeal);
        QueryWrapper<BesSmPro> wrapper = new QueryWrapper<>();
        wrapper.eq("bes_sm_id",bespeakSetMeal.getBesSmId());
        List<BesSmPro> besSmPros = besSmProMapper.selectList(wrapper);
        List<BespeakPoject> bespeakPojects = new ArrayList<>();
        for (BesSmPro b : besSmPros) {
            BespeakPoject poject = pojectMapper.selectById(b.getBesProId());
            bespeakPojects.add(poject);
        }
        for (BespeakPoject p : bespeakPojects) {
            p.setBespeakPojectType(typeMapper.selectById(p.getBesProTypeId()));
        }
        bespeakSetMeal.setList(bespeakPojects);
        return bespeak;
    }

    //查询套餐
    @Override
    public List<BespeakSetMeal> selbespeaksetmeal() {
        QueryWrapper<BespeakSetMeal> wrapper = new QueryWrapper<>();
        wrapper.eq("bes_sm_state",0);
        List<BespeakSetMeal> meals = mealMapper.selectList(wrapper);
        return meals;
    }

    //查询会员
    @Override
    public List<Member> selmember() {
        List<Member> members = memberMapper.selectList(null);
        return members;
    }

    //新增预约
    @Override
    public Boolean addbespeak(Bespeak bespeak) {
        // 从身份证上获取出生日期和年龄
        // 出生日期
        String birthday = bespeak.getBesIdentity().substring(6, 10) + "-"
                + bespeak.getBesIdentity().substring(10, 12) + "-"
                + bespeak.getBesIdentity().substring(12, 14);
        bespeak.setBesBirthday(birthday);
        //年龄
        int year = Calendar.getInstance().get(Calendar.YEAR);
        Integer age = (year - Integer.parseInt(bespeak.getBesIdentity().substring(6, 10)));
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now );
        bespeak.setBesAge(age);
        bespeak.setBesIspay(1);
        bespeak.setBesComplete(0);
        bespeak.setBesIsUpload(0);
        bespeak.setBesCreateTime(time);
        QueryWrapper<SingleDayAppointment> wrapper = new QueryWrapper<>();
        wrapper.eq("sin_day_app_date",bespeak.getBesTime());
        // System.out.println(bespeak);
        SingleDayAppointment appointment = singleMapper.selectOne(wrapper);
        if(appointment == null){
            return false;
        } else {
            bespeak.setBesState(1);
            int insert = bespeakMapper.insert(bespeak);
            if(insert>0){
                appointment.setSinDayAppApp(appointment.getSinDayAppApp()+1);
                singleMapper.updateById(appointment);
                return true;
            }else {
                return false;
            }
        }
    }

    @Override
    public SingleDayAppointment yanzhengTime(String besTime) {
        QueryWrapper<SingleDayAppointment> wrapper = new QueryWrapper<>();
        wrapper.eq("sin_day_app_date",besTime);
        return singleMapper.selectOne(wrapper);
    }


}
